<?php

class MemSubController extends BaseController
{

	/**
	 *编辑部门
	 */
	public function actionAccount()
	{
		if(isset($_GET['title'])){
			//新建分类
			$cmpCat = new CmpCat();
			$cmpCat->zpc_catname = trim($_GET['title']);
			$cmpCat->zpc_type = 4;
			$cmpCat->zm_id = Company::model()->getCmpUserId();
			$cmpCat->inputtime = time();
			if($cmpCat->save()){
				//成功跳转
				$this->redirect(array('account'));
			}
		}
		
		if(isset($_POST['setaction'])){
			$setaction = $_POST['setaction'];
			$staff = $_POST['staff'];
			foreach($setaction as $key=>$val){
				$model = CmpCat::model()->findByPk($key);
				$model->zpc_catname = trim($val);
				$model->save();
			}
			foreach($staff as $key=>$val){
				$model = Adminer::model()->findByPk($key);
				$model->zda_name = trim($val);
				$model->save();
			}
			//成功跳转
			$this->redirect(Yii::app()->request->urlReferrer);
		}
		
		$model = new Adminer();
		
		//获取商家的部门
		$section = CmpCat::model()->getCmpAccount();
		$this->render('account',array(
				'section'=>$section,
				'model'=>$model,
		));
	}

	/**
	 * 删除部门
	 */
	public function actionDeleteAccount($id){
		//部门下有员工，不允许删除
		$criteria=new CDbCriteria;
		$criteria->compare('zpc_id',$id);
		if(Adminer::model()->exists($criteria)){
			//成功跳转
			$this->render('../index/error',array('message'=>'抱歉，部门下有员工,不能删除，请先转移员工！'));
			exit;
		}
		$account = CmpCat::model()->findByPk($id);
		if($account->delete()){
			//成功跳转
			$this->redirect(array('account'));
		}else{
			//失败跳转
			$this->render('../index/error',array('message'=>'对不起，删除部门失败，请稍后再试！'));
			exit;
		}
	}
	
	/**
	 * 创建员工
	 * */
	public function actionCreateStaff()
	{	
		$model=new Adminer;
		
		//设置表单场景
		$model->scenario = "register";
		
		 //Uncomment the following line if AJAX validation is needed
		 $this->performAjaxValidation($model);
		
		 //保存信息
		if(isset($_POST['Adminer']))
		{
			$model->attributes=$_POST['Adminer'];
			if($model->save()){
				//删除旧的角色权限
				AdmGlog::model()->deleteAllByAttributes(array('userid'=>$model->zda_id));
				//生成角色权限
				if(!empty($model->zag_id)){
					$memglog = new AdmGlog();
					$memglog->userid = $model->zda_id;
					$memglog->itemname = $model->zag_id;
					$memglog->inputtime = time();
					$memglog->bizrule = '';
					$memglog->save();
				}
				//成功跳转
				$this->redirect(array('account'));
			}else{
				//成功跳转
				$this->render('../index/success',array(
						'message'=>'恭喜，员工创建失败，请稍后再试！',
						'links'=>array(
								array('组织管理',$this->pre_module_url.'/memSub/account'),
						),
				));
				exit;
			}
		}else{
			throw new CHttpException(404,'The requested page does not exist.');
		}
	}
	
	/**
	 * 修改员工
	 */
	public function actionUpdateStaff(){
		$id = isset($_GET['id'])?(int)$_GET['id']:(int)$_POST['Adminer']['zda_id'];
		//实例化员工
		$model = Adminer::model()->findByPk($id);
		//声明场景
		$model->scenario = 'memsub';
		
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		
		//保存信息
		if(isset($_POST['Adminer']))
		{
			$model->attributes=$_POST['Adminer'];
			if($model->save()){
				//删除旧的角色权限
				AdmGlog::model()->deleteAllByAttributes(array('userid'=>$model->zda_id));
				//生成角色权限
				if(!empty($model->zag_id)){
					$memglog = new AdmGlog();
					$memglog->userid = $model->zda_id;
					$memglog->itemname = $model->zag_id;
					$memglog->inputtime = time();
					$memglog->bizrule = '';
					$memglog->save();
				}
				//成功跳转
				$this->redirect(array('account'));
			}else{
				//失败跳转
				$this->render('../index/error',array(
						'message'=>'恭喜，员工修改失败，请稍后再试！',
						'links'=>array(
								array('组织管理',$this->pre_module_url.'/memSub/account'),
						),
				));
				exit;
			}
		}
		
		//查找员工权限
		$criteria = new CDbCriteria;
		$criteria->compare('userid',$model->zda_id);
		$criteria->select = 'itemname';
		$memglog = AdmGlog::model()->findAll($criteria);
		foreach ($memglog as $val){
			$actions[] = $val->itemname;
		}
		//渲染视图
		$this->render('update_staff',array(
				'actions'=>isset($actions)?$actions:array(),
				'model'=>$model,
		));
	}
	
	/**
	 * 删除员工
	 */
	public function actionDeleteStaff($id){
		
		$model = Adminer::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		
		//删除子账户权限记录
		$criteria = new CDbCriteria;
		$criteria->compare('userid',$model->zda_id);
		AdmGlog::model()->deleteAll($criteria);
		
		//删除子账户
		if($model->delete()){
			//成功跳转
			$this->redirect($this->pre_module_url.'/memSub/account');
		}
	}
	
	/**
	 * 重置员工密码
	 */
	public function actionResetPassword($id){
		
		$model = Adminer::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		//将密码置空
		$model->zda_password = "";
		//场景设为重置密码
		$model->scenario = "reset_password";
		//保存信息
		if(isset($_POST['Adminer']))
		{
			$model->attributes=$_POST['Adminer'];
			$model->zda_password = $model->passwordEncode($model->zda_password);
			$model->re_password = $model->passwordEncode($model->re_password);
			//删除子账户
			if($model->save()){
				//成功跳转
				$this->redirect(array('account'));
			}else{
				print_r($model);exit;
				//失败跳转
				$this->render('../index/error',array('message'=>'对不起，员工密码重置失败，请稍后再试！',));
				exit;
			}
		}
		
		//渲染视图
		$this->render('reset_password',array(
				'model'=>$model,
		));
	}
	
	/**
	 * ajax校验
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='adminer-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}

}
